/*
 * @Author: silence
 * @Date: 2022-11-18 16:35:00
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-27 18:53:55
 * @Description: 
 */
import 'package:bot_toast/bot_toast.dart';
import 'package:tencent_trtc_cloud/trtc_cloud.dart';
import 'package:tencent_trtc_cloud/trtc_cloud_def.dart';
import 'package:tencent_trtc_cloud/trtc_cloud_listener.dart';
import 'package:tencent_trtc_cloud/tx_audio_effect_manager.dart';
import 'package:tencent_trtc_cloud/tx_beauty_manager.dart';
import 'package:tencent_trtc_cloud/tx_device_manager.dart';

class TrtcUtils {
  static TRTCCloud? trtcCloud;
  static TXDeviceManager? txDeviceManager;
  static TXBeautyManager? txBeautyManager;
  static TXAudioEffectManager? txAudioManager;
  static void Function(TRTCCloudListener, dynamic)? onRtcListener;

  static init() async {
    trtcCloud = await TRTCCloud.sharedInstance();
    txDeviceManager = trtcCloud?.getDeviceManager();
    txBeautyManager = trtcCloud?.getBeautyManager();
    txAudioManager = trtcCloud?.getAudioEffectManager();
  }

  /// 进入房间
  /// sdkAppId
  /// userId 用户id
  /// userSig 用户签名
  /// roomId 房间id
  /// role 角色
  static entryRoom(
      {required int sdkAppId,
      required String userId,
      required String userSig,
      required int roomId,
      int role = TRTCCloudDef.TRTCRoleAnchor}) {
    trtcCloud?.enterRoom(
        TRTCParams(
            sdkAppId: sdkAppId,
            userId: userId,
            userSig: userSig,
            roomId: roomId,
            role: role),
        TRTCCloudDef.TRTC_APP_SCENE_VIDEOCALL);
  }

  /// 退出房间
  static exitRoom() {
    trtcCloud?.exitRoom();
  }

  /// 注册侦听
  static registerListenerHanlder() {
    if (onRtcListener == null) {
      onRtcListener = (type, param) {
        if (type == TRTCCloudListener.onError) {
          BotToast.showText(text: '进入房间失败');
        }

        if (type == TRTCCloudListener.onEnterRoom) {
          if (param > 0) {
            BotToast.showText(text: '进入房间成功');
          }
        }

        if (type == TRTCCloudListener.onExitRoom) {
          BotToast.showText(text: '退出房间成功');
        }

        if (type == TRTCCloudListener.onUserVideoAvailable) {
          BotToast.showText(text: '可以开始视频会议了');
        }
      };
    }

    trtcCloud?.registerListener(onRtcListener!);
  }

  /// 移除侦听
  static removeRegisterListener() {
    if (onRtcListener != null) trtcCloud?.unRegisterListener(onRtcListener!);
  }
}
